/*
 * Package il.ac.biu.cs.grossmm.test.fooClients
 * File PingClient.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.fooClients;

import java.io.IOException;

import il.ac.biu.cs.grossmm.foo.format.*;
import il.ac.biu.cs.grossmm.foo.format.PresenceDoc.Tuple;
import il.ac.biu.cs.grossmm.foo.protocol.*;

public class PingClient extends SingleClient {
	static long t = 0;
	
	static final int RUN = 30;
	static final int DELAY = 200;
	int run = 0;
	long pr=0, nr=0, del=0; 
	
	boolean log_requests = false;
	boolean log_responses = false;
	

	String timeStr(long t0, long t1) {
		float diff = (t1 - t0)/1000000f;
		
		return "\n----------------------------------------------------- time: " + diff + " ms";	
	}
	
	long time(long t0, long t1) {
		return (t1-t0)/1000000;
	}
	
	@Override
	protected Response requestReceived(Request request)
	{
		long t1 = System.nanoTime();
		
		if( run++ < RUN ) {
			pr += Flooder.publish_rate;
			nr += Flooder.notify_rate;
			del += time(t, t1);
		} else {
			
			System.out.println(""+pr/RUN+"  "+nr/RUN+"  "+del/RUN);
			pr=0; nr=0; del=0;
			run=0;
		}
		
		if( log_responses)
			System.out.println("<=== "+request + timeStr(t, t1));
		
		if (!(request instanceof NotifyRequest)) {
			System.out.println("Not a notification");
			System.out.println("---> "+ResponseCodes.BAD_REQUEST);
			return new Response(request, ResponseCodes.BAD_REQUEST);
		}
		NotifyRequest nr = (NotifyRequest) request;

		if( log_responses)
			System.out.println("---> 200 ("+request.getCommand()+")");

		return request.okResponse();
	}
	
	void send(Request request) throws IOException, InterruptedException
	{
		if( log_requests )
			System.out.println("===> " + request);

		long t0 =System.nanoTime();
		Response response = sendRequest(request);
		long t1 =System.nanoTime();
		
		if( log_requests )
			System.out.println("<--- " + response + "("+request.getCommand()+")" + time(t0, t1)+"\n");

	}

	public static void main(String[] args) throws IOException, InterruptedException
	{
		PingClient client = new PingClient();
		
		client.connectTo("localhost", 4004);

		System.out.println("Connected.");

		Response resp;

		// LOGIN ALICE AND BOB
		
		client.send(new HelloRequest("Bob", "boob"));
		client.send(new HelloRequest("Alice", "al11z"));


		
		
		// PUBLISH PRIVACY POLICY DOC FOR ALICE: ALLOW ALL
		
		WatcherRulesDoc rdoc = new WatcherRulesDoc();
		rdoc.addPersonalRules(null, null, true);
		// rdoc.addServiceRules(null, null, null, true, true, false);


		client.send(new PublishRequest("0", "Alice", 1800,
			WatcherRulesDoc.CONTENT_TYPE, rdoc.serialize()));
		
		
		// PUBLISH INITIAL PRESENCE DOC FOR ALICE
		
		Tuple t2 =
			new Tuple("124", .1f, "me@kitchen.home.com", OnlineStatus.OFFLINE,
				"personal", Capabilities.of("voice"));

		Tuple t3 =
			new Tuple("129", .8f, "me@kitchen.home.com", OnlineStatus.ONLINE,
				"personal", Capabilities.of("video"));

		PresenceDoc foo =
			new PresenceDoc("Alice", new Mood(1, 8, 3, 6), new Activities(Activity.MEAL,
				Activity.HOLIDAY), "kitchen", "home", t2, t3);

		client.send(new PublishRequest("1", "Alice", 1800, PresenceDoc.CONTENT_TYPE,
			foo.serialize()));
		
		
		
		// SUBSCRIBE BOB TO PRESENCE INFORMATION ABOUT ALICE
		
		client.send(new SubscribeRequest("Bob", "Alice", 60));
		
		String[] loc = new String[] {"kitchen", "room" };
		int d = 0;
		
		// ALTER PRESENCE OF ALICE
		while( true ) {
			foo.setLocation(loc[d=1-d]);
			String str = foo.serialize();
			
			t = System.nanoTime();
			PublishRequest request = new PublishRequest("1", "Alice", 1800, 
								PresenceDoc.CONTENT_TYPE,
							str);
			client.send(request);
			
			Thread.sleep(DELAY);
		}
	}
}
